﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BackEnd.Facade.Implementations;
using BackEnd.Facade.Interfaces;
using System.Threading;
using WebMVC.Models;

namespace WebMVC.Controllers
{
    public abstract class ControllerBase : Controller
    {
        protected IMembershipManager _membershipManager;

        public ControllerBase(IMembershipManager membershipManager)
        {
            _membershipManager = membershipManager;
        }
        public ControllerBase()
            : this(new MembershipManager())
        {
        }

        protected Culture CurrentCulture
        {
            get
            {
                var name = Thread.CurrentThread.CurrentCulture.Name.Substring(0, 2);
                return (Culture)Enum.Parse(typeof(Culture), name);
            }
        }

        protected Guid CurrentUserId
        {
            get
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    return new Guid(Membership.GetUser().ProviderUserKey.ToString());
                }
                else
                {
                    return _membershipManager.GetAnonymousUserId(HttpContext.Profile.UserName);
                }
            }
        }

        protected string GetLocalizedName(dynamic obj)
        {
            switch (CurrentCulture)
            {
                case Culture.en:
                    return obj.NameEN;
                case Culture.ru:
                    return obj.NameRU;
                case Culture.hy:
                    return obj.NameAM;
            }
            return null;
        }

        protected string GetLocalizedDescription(dynamic obj)
        {
            switch (CurrentCulture)
            {
                case Culture.en:
                    return obj.DescriptionEN;
                case Culture.ru:
                    return obj.DescriptionRU;
                case Culture.hy:
                    return obj.DescriptionAM;
            }
            return null;
        }
    }
}